package com.chaoyong.higo.activity;

import java.util.List;

import org.apache.http.entity.StringEntity;
import org.json.JSONObject;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.chaoyong.higo.R;
import com.chaoyong.higo.adapter.AccountAdapter;
import com.chaoyong.higo.base.BaseActivity;
import com.chaoyong.higo.bean.FragAccountBean;
import com.chaoyong.higo.bean.MyAccountBean;
import com.chaoyong.higo.net.OApi;
import com.chaoyong.higo.utils.EmptyUtil;
import com.chaoyong.higo.utils.JLog;
import com.chaoyong.higo.utils.PrefUtils;
import com.chaoyong.higo.utils.V;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

/**
 * 我的账户
 * 
 * @author zk
 *
 */
public class MyAccountActivity extends BaseActivity {

	private static final String ClassName = MyAccountActivity.class.getName();

	private LinearLayout base_left_iv;// 左侧返回按钮
	private TextView base_title_tv;// 标题
	private TextView base_right_tv;// 标题右侧文字

	private TextView my_account_counts;// 账户余额
	/** 充值总额 */
	private TextView my_account_chongzhi;
	/** 消费总额 */
	private TextView my_account_buy;

	private ListView account_list_view;
	private AccountAdapter adapter;

	/** 充值 */
	private Button acc_header_chongZhi;

	String account_amount;
	
	private List<MyAccountBean.DataEntity.ListEntity> eList;

	@Override
	public int layoutResID() {
		return R.layout.activity_my_account;
	}

	@Override
	public void findView() {
		findTitleIds();
		account_list_view = (ListView) findViewById(R.id.account_list_view);
		acc_header_chongZhi = (Button) findViewById(R.id.acc_header_chongZhi);

		my_account_counts = V.f(this, R.id.my_account_counts);
		my_account_chongzhi = V.f(this, R.id.my_account_chongzhi);
		my_account_buy = V.f(this, R.id.my_account_buy);
	}

	@Override
	public void init() {
		base_left_iv.setOnClickListener(this);
		acc_header_chongZhi.setOnClickListener(this);

		// RelativeLayout hearderViewLayout = (RelativeLayout)
		// LayoutInflater.from(this)
		// .inflate(R.layout.account_header, null);
		// account_list_view.addHeaderView(hearderViewLayout);

		adapter = new AccountAdapter(this);
		account_list_view.setAdapter(adapter);

		/*account_amount = getIntent().getStringExtra("account_amount");
		if (!EmptyUtil.isEmpty(account_amount)) {
			my_account_counts.setText("￥" + account_amount + "");
		} else {
			my_account_counts.setText("--");
		}*/
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.base_left_iv:// 返回
			this.finish();
			break;
		case R.id.acc_header_chongZhi:// 充值
			// showToast("充值");
			startActivity(new Intent(this, RecargeActivity.class));
			break;
		}
	}

	private void findTitleIds() {
		base_left_iv = (LinearLayout) findViewById(R.id.base_left_iv);
		base_title_tv = (TextView) findViewById(R.id.base_title_tv);
		base_right_tv = (TextView) findViewById(R.id.base_right_tv);

		base_title_tv.setText("我的账户");
		base_right_tv.setVisibility(View.GONE);
	}

	@Override
	protected void onStart() {
		super.onStart();
		String uid = PrefUtils.getString(MyAccountActivity.this, "userId", "1");
		// httpQueryMyAccountInfo("1", "15");
		httpYue(uid);
		httpQueryMyAccountInfo(uid, "15");
	}

	/**
	 * 用户账户信息
	 */
	private void httpYue(String uid) {
		HttpUtils utils = new HttpUtils();
		final RequestParams param = new RequestParams();
		try {
			JSONObject jsonObject = new JSONObject();
			JSONObject info = new JSONObject();

			jsonObject.putOpt("model", "Members");
			jsonObject.putOpt("function", "showInfoApp");
			jsonObject.putOpt("info", info);

			info.putOpt("uid", uid);

			param.setBodyEntity(new StringEntity(jsonObject.toString(), "UTF-8"));
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		utils.send(HttpMethod.POST, OApi.Appport_appMember, param, new RequestCallBack<String>() {

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				JLog.i(ClassName, arg0.getMessage() + "\n" + "上传数据："+param.toString());
			}

			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				//JLog.i(ClassName, arg0.result);
				Gson gson = new Gson();
				String result = arg0.result;
				FragAccountBean bean = gson.fromJson(result, FragAccountBean.class);
				String status = bean.getStatus();
				if (status.equals("1")) {
					FragAccountBean.DataEntity entities = bean.getData();
					if (entities != null) {
						String aa = entities.getAccount_amount();
						if (!EmptyUtil.isEmpty(aa)) {
							my_account_counts.setText("￥" + aa);
						}else{
							my_account_counts.setText("￥0.00");
						}
					}
				}
			}
		});
	}

	private void httpQueryMyAccountInfo(String uid, String limit) {
		HttpUtils utils = new HttpUtils();
		final RequestParams param = new RequestParams();
		try {
			JSONObject jsonObject = new JSONObject();
			JSONObject info = new JSONObject();

			jsonObject.putOpt("model", "MoneyTypeLog");
			jsonObject.putOpt("function", "memberAccount");
			jsonObject.putOpt("info", info);

			info.putOpt("uid", uid);
			info.putOpt("limit", limit);

			param.setBodyEntity(new StringEntity(jsonObject.toString(), "UTF-8"));
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		utils.send(HttpMethod.POST, OApi.Appport_appMember, param, new RequestCallBack<String>() {

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				JLog.i(ClassName, arg0.getMessage() + "\n" + "上传数据：" + param.toString());
			}

			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				JLog.i(ClassName, arg0.result);
				Gson gson = new Gson();
				MyAccountBean bean = gson.fromJson(arg0.result, MyAccountBean.class);
				String status = bean.getStatus();
				if (status.equals("1")) {
					MyAccountBean.DataEntity.XiaofeiEntity entity = bean.getData().getXiaofei();
					if (entity != null) {
						setXiaoFeiData(entity);
					}

					eList = bean.getData().getList();
					//adapter.addAllDataAndNorify(eList);
					adapter.setList(eList);
					adapter.notifyDataSetChanged();
				}
			}
		});
	}

	/**
	 * 设置充值总额与消费总额
	 * 
	 * @param entity
	 */
	private void setXiaoFeiData(MyAccountBean.DataEntity.XiaofeiEntity entity) {
		String recharge = entity.getRecharge();
		String xiaofei = entity.getXiaofei();
		if (!EmptyUtil.isEmpty(recharge)) {
			my_account_chongzhi.setText("￥" + recharge);
		} else {
			my_account_chongzhi.setText("￥0.00");
		}

		if (!EmptyUtil.isEmpty(xiaofei)) {
			my_account_buy.setText("￥" + xiaofei);
		} else {
			my_account_buy.setText("￥0.00");
		}
	}
}
